<template>
  <!-- <div class="login-container">
    <el-row>
      <el-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" auto-complete="off" class="login-form" label-position="left">
          <div class="title">
            hello !
          </div>
          <div class="title-tips">欢迎来到{{ title }}！</div>
          <el-form-item style="margin-top: 40px;" prop="username">
            <span class="svg-container svg-container-admin">
              <vab-icon :icon="['fas', 'user']" />
            </span>
            <el-input v-model.trim="loginForm.username" v-focus auto-complete="off" placeholder="请输入用户名" tabindex="1" type="text" />
          </el-form-item>
          <el-form-item prop="password">
            <span class="svg-container svg-container-pass"><vab-icon :icon="['fas', 'lock']" /></span>
            <el-input :key="passwordType" ref="password" v-model.trim="loginForm.password" :type="passwordType" auto-complete="off" placeholder="请输入密码" tabindex="2" @keyup.enter.native="handleLogin" />
            <span v-if="passwordType === 'password'" class="show-pwd" @click="showPwd"><vab-icon :icon="['fas', 'eye-slash']" /></span>
            <span v-else class="show-pwd" @click="showPwd"><vab-icon :icon="['fas', 'eye']" /></span>
          </el-form-item>
          <el-button :loading="loading" class="login-btn" type="primary" @click="handleLogin">登录 </el-button>
          <router-link to="/register">
            <div style="margin-top: 20px;">注册</div>
          </router-link>
        </el-form>
      </el-col>
    </el-row>
  </div> -->
  <div class="login-container">
    <div class="login-box">
      <!-- 登录表单区域 -->
      <p>电商后台管理系统</p>
      <el-form class="login-form" ref="loginForm" :model="loginForm" :rules="loginRules">
        <div>登录</div>
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" v-focus v-model="loginForm.username"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" v-model="loginForm.password" show-password @keyup.enter.native="handleLogin"></el-input>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="remeberLoginStatus">记住账号</el-checkbox>
        </el-form-item>
        <el-form-item class="login-btn">
          <el-button type="primary" @click="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="copyright">
      <span>Copyright © 2018 - 2020 恒裁(hensay.com) -</span>
      <a href="http://www.beian.miit.gov.cn/" target="_blank">浙ICP备20025019号-1</a>
    </div>
  </div>
</template>

<script>
import { isPassword } from '@/utils/validate'
import { setAccessToken } from '@/utils/accessToken'
export default {
  name: 'Login',
  directives: {
    focus: {
      inserted(el) {
        el.querySelector('input').focus()
      },
    },
  },
  data() {
    const validateUserName = (rule, value, callback) => {
      if ('' == value) {
        callback(new Error('用户名不能为空'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (!isPassword(value)) {
        callback(new Error('密码不能少于6位'))
      } else {
        callback()
      }
    }
    return {
      nodeEnv: process.env.NODE_ENV,
      title: this.$baseTitle,
      loginForm: {
        username: '',
        password: '',
        vertify: '123',
      },
      loginRules: {
        username: [
          {
            required: true,
            trigger: 'blur',
            validator: validateUserName,
          },
        ],
        password: [
          {
            required: true,
            trigger: 'blur',
            validator: validatePassword,
          },
        ],
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      remeberLoginStatus: true,
    }
  },
  watch: {
    $route: {
      handler(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true,
    },
  },
  mounted() {
    if ('production' !== process.env.NODE_ENV) {
      this.loginForm.username = 'vaevie'
      this.loginForm.password = '123456'
    }
  },
  methods: {
    showPwd() {
      this.passwordType === 'password' ? (this.passwordType = '') : (this.passwordType = 'password')
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          this.loading = true
          // console.log('提交了')
          await this.$store.dispatch('user/login', this.loginForm)
          // setAccessToken('admin-accessToken')
          const routerPath = '/'

          // let routerPath = this.redirect === '/404' || this.redirect === '/401' ? '/' : this.redirect

          // console.log(routerPath)
          this.$router.push({ path: routerPath }).catch(error => {
            // console.log(error)
          })
          this.loading = false
        } else {
          return false
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
// .login-container {
//   height: 100vh;
//   min-height: 600px;
//   background: url('~@/assets/login_images/background.jpg') center center fixed no-repeat;
//   background-size: cover;

//   .title {
//     height: 50px;
//     font-size: 54px;
//     font-weight: 500;
//     color: rgba(14, 18, 26, 1);
//   }

//   .title-tips {
//     height: 24px;
//     margin-top: 29px;
//     font-size: 26px;
//     font-weight: 400;
//     color: rgba(14, 18, 26, 1);
//   }

//   .login-btn {
//     display: inherit;
//     width: 220px;
//     height: 60px;
//     margin-top: 5px;
//     border: 0;

//     &:hover {
//       opacity: 0.9;
//     }
//   }

//   .login-form {
//     position: relative;
//     max-width: 100%;
//     margin: 22vh 10% 10%;
//     overflow: hidden;

//     .forget-password {
//       width: 100%;
//       margin-top: 40px;
//       text-align: left;

//       .forget-pass {
//         width: 129px;
//         height: 19px;
//         font-size: 20px;
//         font-weight: 400;
//         color: rgba(92, 102, 240, 1);
//       }
//     }
//   }

//   .tips {
//     margin-bottom: 10px;
//     font-size: $base-font-size-default;
//     color: $base-color-white;

//     span {
//       &:first-of-type {
//         margin-right: 16px;
//       }
//     }
//   }

//   .title-container {
//     position: relative;

//     .title {
//       margin: 0 auto 40px auto;
//       font-size: 34px;
//       font-weight: bold;
//       color: $base-color-blue;
//       text-align: center;
//     }
//   }

//   .svg-container {
//     position: absolute;
//     top: 3px;
//     left: 15px;
//     z-index: $base-z-index;
//     font-size: 16px;
//     color: #d7dee3;
//     cursor: pointer;
//     user-select: none;
//   }

//   .show-pwd {
//     position: absolute;
//     top: 3px;
//     right: 25px;
//     font-size: 16px;
//     color: $base-font-color;
//     cursor: pointer;
//     user-select: none;
//   }

//   ::v-deep {
//     .el-form-item {
//       padding-right: 0;
//       margin: 20px 0;
//       color: #454545;
//       background: transparent;
//       border: 1px solid transparent;
//       border-radius: 2px;

//       &__content {
//         min-height: $base-input-height;
//         line-height: $base-input-height;
//       }

//       &__error {
//         position: absolute;
//         top: 100%;
//         left: 18px;
//         font-size: $base-font-size-small;
//         line-height: 18px;
//         color: $base-color-red;
//       }
//     }

//     .el-input {
//       box-sizing: border-box;

//       input {
//         height: 58px;
//         padding-left: 45px;
//         font-size: $base-font-size-default;
//         line-height: 58px;
//         color: $base-font-color;
//         background: #f6f4fc;
//         border: 0;
//         caret-color: $base-font-color;
//       }
//     }
//   }
// }

.login-container {
  position: relative;
  width: 100%;
  overflow: hidden;
  min-height: 100vh;
  background-color: #f0f2f5;
  background-image: url();
  background-position: 50%;
  background-repeat: no-repeat;
}
.login-box {
  position: absolute;
  left: 50%;
  top: 46%;
  transform: translate(-50%, -50%);
  width: 800px;
  height: 600px;
  border-radius: 10px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.08);
  background: url('~@/assets/login_images/bg.jpg') no-repeat;
  & > p:nth-child(1) {
    position: absolute;
    left: 70px;
    top: 50%;
    transform: translateY(-50%);
    margin: 0;
    font-size: 20px;
    color: #fff;
  }
}
.login-form {
  border-radius: 0 10px 10px 0;
  margin-left: 300px;
  padding: 100px 60px;
  height: 600px;
  box-sizing: border-box;
  background-color: #ffffff;
  & > div:nth-child(1) {
    text-align: center;
    font-size: 16px;
    // font-weight: 600;
    letter-spacing: 6px;
    margin-bottom: 90px;
    color: #777;
  }
}
.login-btn {
  text-align: center;
  .el-button {
    width: 160px !important;
    border-radius: 36px;
  }
}
.copyright {
  position: absolute;
  bottom: 30px;
  left: 50%;
  display: flex;
  align-items: center;
  transform: translateX(-50%);
  font-size: 13px;
  color: #999;
  a {
    margin: 0 4px;
    color: #999;
    text-decoration: none;
    // line-height: 24px;
  }

  p {
    margin-bottom: 8px;
    span {
      margin: 0 6px;
      cursor: pointer;
    }
  }
}
</style>
